//
//  SecondShotController.m
//  yameishe3
//
//  Created by pro on 2017/3/13.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "SecondShotController.h"
#import "SecondShotAccountCell.h"
@interface SecondShotController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _page;
    __weak IBOutlet UITableView *_tableView;
    NSMutableArray *_arrDataSource;
    NSMutableArray *_arrRemoverow;
    __weak IBOutlet UIButton *_selectAllBtn;
    __weak IBOutlet UIView *_maskView;
    __weak IBOutlet UIView *_bottomView;
    __weak IBOutlet UILabel *_selectCountLab;
}
@end

@implementation SecondShotController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self controllerSetting];
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    [_tableView registerNib:[UINib nibWithNibName:@"SecondShotAccountCell"bundle:nil] forCellReuseIdentifier:@"SecondShotAccountCell"];
    [_tableView addHeaderWithTarget:self action:@selector(reloadData) dateKey:@"table"];
    [_tableView addFooterWithTarget:self action:@selector(loadMoreData)];
    _arrRemoverow = [[NSMutableArray alloc]init];
    _arrDataSource = [[NSMutableArray alloc]init];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(50+64);
        make.leading.trailing.bottom.equalTo(self.view);
    }];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(50));
        make.leading.trailing.bottom.equalTo(self.view);
    }];
     [_maskView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTouchMaskView)]];

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden=YES;
//    [self.view rlayoutIfNeeded];
    //    self.hidesBottomBarWhenPushed = YES;
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    self.tabBarController.tabBar.hidden=YES;
    //    self.hidesBottomBarWhenPushed = YES;
    
}
-(void)reloadData
{
    _page = 1;
}

-(void)loadMoreData
{
    _page++;

}

- (IBAction)onTouchSelectedAll:(UIButton *)sender {
    if (sender.selected == YES) {//重置
        [_arrRemoverow removeAllObjects];
      
    }
    else{
        _arrRemoverow = [NSMutableArray arrayWithArray:_arrDataSource.copy];
           }
    BOOL selected = sender.selected;
    sender.selected = selected?NO:YES;
  
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
    return _arrDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger curRow = indexPath.row;
//    NSDictionary *model = _arrDataSource[curRow];
    
    SecondShotAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondShotAccountCell" forIndexPath:indexPath];
    
    [cell setDataSourceWithModel:nil isChoose:YES isSelected:[self isSelectedCurrentRow:curRow] isHistory:YES];
    [cell setCallBackOnTouchSelectBtn:^(BOOL isSelectedBtn) {
        if (isSelectedBtn) {
            if (![_arrRemoverow containsObject:@(curRow)]) {
                [_arrRemoverow addObject:@(curRow)];
            }
        } else {
            if ([_arrRemoverow containsObject:@(curRow)]) {
                [_arrRemoverow removeObject:@(curRow)];
            }
        }
        
    }];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 330
    ;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    NSDictionary *model = _arrDataSource[indexPath.row];
//    [ADOHistory deleteModelByTitle:model.title fromTb:DB_HistoryName];
//    [ADOHistory addModel:_arrDataSource[indexPath.row] fromTb:DB_HistoryName];
//    UIViewController_Detail_Iphone *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"UIViewController_Detail_Iphone"];
//    controller.model = _arrDataSource[indexPath.row];
//    //    UIControllerBrowser *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"UIControllerBrowser"];
//    //    [self.navigationController pushViewController:controller animated:YES];
//    //    controller.model = _arrDataSource[indexPath.row];
//    [self.navigationController pushViewController:controller animated:YES];
//    
//    
//}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        ModelBase *model = _arrDataSource[indexPath.row];
//        [ADOHistory deleteModelByTitle:model.title fromTb:_currentTypeName];
//        [_arrDataSource removeObjectAtIndex:indexPath.row];
//        // Delete the row from the data source.
//        [_currentTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//        _arrHistory = [[NSMutableArray alloc]initWithArray:[ADOHistory queryAll:DB_HistoryName]];
//        _arrFavor = [[NSMutableArray alloc]initWithArray:[ADOHistory queryAll:DB_FavName]];
//    }
    
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}
- (BOOL)isSelectedCurrentRow:(NSInteger)curRow {
    
    BOOL isSelected = NO;
    
    if (_arrRemoverow.count <= 0) {
        return NO;
    } else {
        NSDictionary *curModel = _arrDataSource[curRow];
        for (NSDictionary *model in _arrRemoverow) {
            if (model == curModel) {
                isSelected = YES;
            }
        }
        
        return isSelected;
    }
}

@end
